package mi.payment.ui.admin;

import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;

import mi.payment.R;
import mi.payment.settings.SettlementTransaction;
import mi.payment.settings.SettlementTransactionOperation;
import mi.payment.ui.base.ParentActivity;
import sunmi.sunmiui.button.ButtonSwitch;
import sunmi.sunmiui.title.Title;

/**
 * 结算交易控制
 * Created by lj on 2017/2/13.
 */
public class SettlementTransactionSettingsActivity extends ParentActivity {
    private View mIsSettlementTransactionFinishExit;
    private ButtonSwitch mIsSettlementTransactionFinishExitSwitch;
    private SettlementTransactionOperation mSettlementTransactionOperation;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settlement_transaction_setting);
        mSettlementTransactionOperation = SettlementTransactionOperation.getInstance();
        initView();
        setClickListener();
        setCheckChangeListener();
        updateUI();
    }

    /**
     * 初始化控件
     */
    private void initView(){
        Title mTitle;
        TextView mLeftText;
        mTitle = (Title) findViewById(R.id.Settlement_transaction_setting_title);
        mTitle.setButtonVisibility(false, true, true, true);
        mTitle.setHeadBgColor(getResources().getColor(R.color.colorBar));
        mTitle.setTitleText(getResources().getString(R.string.settlement_transaction_setting));
        mTitle.setMenuClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SettlementTransactionSettingsActivity.this.finish();
            }
        }, null, null, null);

        mIsSettlementTransactionFinishExit = findViewById(R.id.is_settlement_transaction_finish_exit);

        mLeftText = (TextView) mIsSettlementTransactionFinishExit.findViewById(R.id.switch_item_text);
        mLeftText.setText(getResources().getString(R.string.is_settlement_transaction_finish_exit));


        mIsSettlementTransactionFinishExitSwitch = (ButtonSwitch) mIsSettlementTransactionFinishExit.findViewById(R.id.set_switch);
    }

    /**
     * 设置点击事件
     */
    private void setClickListener(){
        mIsSettlementTransactionFinishExit.setOnClickListener(this);
    }

    /**
     * 设置SwitchButton选择事件
     */
    private void setCheckChangeListener(){
        mIsSettlementTransactionFinishExitSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mSettlementTransactionOperation.saveAutomaticSignOut(isChecked);
            }
        });

        updateUI();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch(v.getId()){
            case R.id.is_settlement_transaction_finish_exit:
                //结算后自动签退
                mIsSettlementTransactionFinishExitSwitch.setOnCheck(!mIsSettlementTransactionFinishExitSwitch.isChecked());
                break;
        }
    }

    private void updateUI(){
        SettlementTransaction settlementTransaction = mSettlementTransactionOperation.getSettlementTransaction();
        mIsSettlementTransactionFinishExitSwitch.setOnCheck(settlementTransaction.isAutomaticSignOut());
    }
}
